package com.takeaway.servlet;


import com.jspsmart.upload.*;
import com.takeaway.const_.Const;
import com.takeaway.pojo.Food;
import com.takeaway.pojo.Shop;
import com.takeaway.pojo.User;
import com.takeaway.service.serviceimpl.BookServiceImpl;
import com.takeaway.service.serviceimpl.FoodServiceImpl;
import com.takeaway.service.serviceimpl.MsgBoardServiceImpl;
import com.takeaway.service.serviceimpl.ShopServiceImpl;
import com.takeaway.service.serviceinterface.BookService;
import com.takeaway.service.serviceinterface.FoodService;
import com.takeaway.service.serviceinterface.MsgBoardService;
import com.takeaway.service.serviceinterface.ShopService;
import com.takeaway.utils.AgentManage;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author 后台赖骏
 */
@SuppressWarnings("unused")
@WebServlet("/business/*")
public class BusinessServlet extends BaseServlet{
    public void addShop(HttpServletRequest req, HttpServletResponse resp)throws Exception{
            req.setCharacterEncoding("utf-8");
        String shopName = req.getParameter("shopName");
        String description = req.getParameter("description");
         User user = (User)req.getSession().getAttribute("user");
        ShopService shopService = new ShopServiceImpl();
        AgentManage agentManage = new AgentManage(shopService);
        ShopService agent = (ShopService)agentManage.getAgent();
            int addShop = agent.addShop(shopName, user.getId(), description);
            if (addShop > 0){
                req.setAttribute("msg", Const.CREATE_SUCCESS_MSG);
                req.getRequestDispatcher("/jsp/Msg.jsp").forward(req,resp);
            }
            else {
                req.setAttribute("msg", Const.CREATE_ERROR_MSG);
                req.getRequestDispatcher("/jsp/Msg.jsp").forward(req,resp);
            }
    }
    public void updateShop(HttpServletRequest req, HttpServletResponse resp)throws Exception{
            req.setCharacterEncoding("utf-8");
        String description = req.getParameter("description");
        User user = (User)req.getSession().getAttribute("user");
        String shopName = req.getParameter("shopName");
         int sentCoupon =Integer.parseInt(req.getParameter("sentCoupon"));
        boolean matches = !(description.length()>0 && shopName.length()>0);
        if (matches){
            req.setAttribute("msg",Const.ILLEGAL_INPUT_MSG);
            try {
                req.getRequestDispatcher("/jsp/Msg.jsp").forward(req,resp);
                return;
            } catch (ServletException | IOException e) {
                e.printStackTrace();
            }
        }
        ShopService shopService = new ShopServiceImpl();
        AgentManage agentManage = new AgentManage(shopService);
        ShopService agent = (ShopService)agentManage.getAgent();
        try {
            int update = agent.updateShop(shopName, user.getId(), description,sentCoupon);
            if (update > 0){
                req.setAttribute("msg", Const.UPDATE_SUCCESS_MSG);
                req.getRequestDispatcher("/jsp/Msg.jsp").forward(req,resp);
            }
        } catch (Exception e) {
            e.printStackTrace();
            req.setAttribute("msg", Const.UPDATE_SUCCESS_MSG);
            try {
                req.getRequestDispatcher("/jsp/Msg.jsp").forward(req,resp);
                    req.setAttribute("msg", Const.UPDATE_ERROR_MSG);
            } catch (ServletException | IOException ex) {
                ex.printStackTrace();
            }
        }
    }
    public void checkShop(HttpServletRequest req, HttpServletResponse resp)throws Exception{
        User user = (User)req.getSession().getAttribute("user");
        ShopService shopService = new ShopServiceImpl();
        AgentManage agentManage = new AgentManage(shopService);
        ShopService agent = (ShopService)agentManage.getAgent();
        Object[] objects = agent.checkShop(user.getId());
        req.setAttribute("shop",objects[0]);
        req.setAttribute("sentCoupon",objects[1]);
            req.getRequestDispatcher("/jsp/ShopManage.jsp").forward(req,resp);
    }
    public void checkBook(HttpServletRequest req, HttpServletResponse resp)throws Exception {
        BookService bookService = new BookServiceImpl();
        AgentManage agentManage = new AgentManage(bookService);
        BookService agent = (BookService)agentManage.getAgent();
        User user = (User) req.getSession().getAttribute("user");
            Object[] objects = agent.checkShopBook(user.getId());
            req.setAttribute("books",objects[0]);
            req.setAttribute("users",objects[1]);
            req.setAttribute("foods",objects[2]);
            req.getRequestDispatcher("/jsp/BookManage.jsp").forward(req,resp);
    }

    public void acceptBook(HttpServletRequest req, HttpServletResponse resp)throws Exception {
        BookService bookService = new BookServiceImpl();
        AgentManage agentManage = new AgentManage(bookService);
        BookService agent = (BookService)agentManage.getAgent();
        User user = (User)req.getSession().getAttribute("user");
        int bookId = Integer.parseInt(req.getParameter("bookId"));
        int conditionCode = Integer.parseInt(req.getParameter("conditionCode"));
        int foodId = Integer.parseInt(req.getParameter("foodId"));
            User newUser = agent.updateBook(conditionCode, bookId, user.getId(),foodId);
            req.getSession().setAttribute("user",newUser);
                req.getRequestDispatcher("business/checkBook").forward(req,resp);
    }
    public void checkMenu(HttpServletRequest req, HttpServletResponse resp)throws Exception{
        FoodService foodService = new FoodServiceImpl();
        AgentManage agentManage = new AgentManage(foodService);
        FoodService agent = (FoodService)agentManage.getAgent();
        int shopId =Integer.parseInt(req.getParameter("shopId"));
            List<Food> foods = agent.showShopFood(shopId);
            req.setAttribute("foods",foods);
            req.setAttribute("shopId",shopId);
            req.getRequestDispatcher("/jsp/MenuManage.jsp").forward(req,resp);
    }
    public void deleteMenu(HttpServletRequest req, HttpServletResponse resp)throws Exception{
        FoodService foodService = new FoodServiceImpl();
        AgentManage agentManage = new AgentManage(foodService);
        FoodService agent = (FoodService)agentManage.getAgent();
        int foodId =Integer.parseInt(req.getParameter("foodId"));
        int delete;
            delete = agent.deleteFood(foodId);
            if (delete >0){
                req.getRequestDispatcher("business/checkMenu").forward(req,resp);
            }
    }
    public void updateMenu(HttpServletRequest req, HttpServletResponse resp)throws Exception{
            req.setCharacterEncoding("utf-8");
        FoodService foodService = new FoodServiceImpl();
        AgentManage agentManage = new AgentManage(foodService);
        FoodService agent = (FoodService)agentManage.getAgent();
        int foodId =Integer.parseInt(req.getParameter("foodId"));
        String disName = req.getParameter("disName");
        double price = Double.parseDouble(req.getParameter("price"));
        String description = req.getParameter("description");
        boolean matches = description.length()>0&&disName.length()>0;
        if (!matches){
            req.setAttribute("msg",Const.ILLEGAL_INPUT_MSG);
                req.getRequestDispatcher("/jsp/Msg.jsp").forward(req,resp);
                return;
        }
           int update = agent.updateFood(foodId,disName,description,price);
            if (update >0){
                req.getRequestDispatcher("business/checkMenu").forward(req,resp);
            }
    }
    public void addMenu(HttpServletRequest req, HttpServletResponse resp)throws Exception{
            req.setCharacterEncoding("utf-8");
        FoodService foodService = new FoodServiceImpl();
        AgentManage agentManage = new AgentManage(foodService);
        FoodService agent = (FoodService)agentManage.getAgent();
        String disName = req.getParameter("disName");
        double price = Double.parseDouble(req.getParameter("price"));
        int shopId =Integer.parseInt(req.getParameter("shopId"));
        String description = req.getParameter("description");
            int addFood = agent.addFood(disName, description,price,shopId);
            if (addFood>0){
                req.getRequestDispatcher("business/checkMenu").forward(req, resp);
            }
            else {
                req.setAttribute("msg",Const.ADD_ERROR_MSG);
                req.getRequestDispatcher("/jsp/Msg.jsp").forward(req, resp);
            }
    }
    public void deleteMsg(HttpServletRequest req, HttpServletResponse resp)throws Exception{
        MsgBoardService msgBoardService = new MsgBoardServiceImpl();
        AgentManage agentManage = new AgentManage(msgBoardService);
        MsgBoardService agent = (MsgBoardService)agentManage.getAgent();
        int msgId = Integer.parseInt(req.getParameter("msgId"));
        int shopId = Integer.parseInt(req.getParameter("shopId"));
            int deleteMsg = agent.deleteMsg(msgId);
            if (deleteMsg>0){
                req.getRequestDispatcher("../user/checkMsg").forward(req,resp);
            }


    }
    public void upLoadImg(HttpServletRequest req, HttpServletResponse resp)throws Exception{
            SmartUpload smartUpload = new SmartUpload();
            smartUpload.initialize(this.getServletConfig(),req,resp);
            smartUpload.upload();
            Files files = smartUpload.getFiles();
            File file = files.getFile(0);
            Request request = smartUpload.getRequest();
            int foodId = Integer.parseInt(request.getParameter("foodId"));
            String fileName = file.getFileName();
            smartUpload.save("/images/shop");
            System.out.println(fileName);
            FoodService foodService = new FoodServiceImpl();
            AgentManage agentManage = new AgentManage(foodService);
            FoodService agent = (FoodService)agentManage.getAgent();
                int update = agent.updatePhoto(foodId, fileName);
                if (update>0){
                    req.setAttribute("msg",Const.ADD_SUCCESS_MSG);
                    req.getRequestDispatcher("/jsp/Msg.jsp").forward(req,resp);
                }

    }
}


